import csv
from datetime import datetime

from matplotlib import pyplot as plt

# 文件名
filename = 'death_valley_2014.csv'

# 打开文件
with open(filename) as f:
    # csv格式文档读取
    reader = csv.reader(f)
    # 读取文档的下一行，此处位首行，即为列名称
    header_row = next(reader)

    # 分别记录日期，最高的最低温度
    dates, highs, lows = [], [], []
    # 遍历数据行，第一行已经读取完了
    for row in reader:
        # 捕获数据异常
        try:
            current_date = datetime.strptime(row[0], "%Y-%m-%d")
            high = int(row[1])
            low = int(row[3])
        except ValueError:
            print(current_date, 'missing data')
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)

# Plot data.
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red', alpha=0.5)
plt.plot(dates, lows, c='blue', alpha=0.5)
# 在最搞温度和最低温度之间填充蓝色
plt.fill_between(dates, highs, lows, facecolor='blue', alpha=0.1)

# Format plot.
# 设置标题
title = "Daily high and low temperatures - 2014\nDeath Valley, CA"
plt.title(title, fontsize=20)
plt.xlabel('', fontsize=16)
# 设置自动控制x轴数据间距
fig.autofmt_xdate()
plt.ylabel("Temperature (F)", fontsize=16)
#
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()
